import java.rmi.server.RemoteRef;
import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 28779
 * Date: 2023-05-16
 * Time: 9:38
 */
public class Sort_practice {
    /**
     * 插入排序
     * @param array
     */
    private static void insertSort(int[] array) {
        for (int i = 1; i <= array.length - 1; i++) {
            int temp = array[i];
            int j = i - 1;
            for ( ; j >= 0 ; j--) {
                if(array[j] > temp) {
                    array[j + 1] = array[j];
                } else {
                    break;
                }
            }
            array[j + 1] = temp;
        }
    }

    /**
     * 希尔排序
     * @param array
     */
    private static void shellSort(int[] array) {
        int gap = array.length;
        while(gap > 0) {
            gap /= 2;
            Shell(array, gap);
        }
    }
    private static void Shell(int[] array, int gap) {
        for (int i = gap; i < array.length; i++) {
            int temp = array[i];
            int j = i - gap;
            for ( ; j >= 0 ; j -= gap) {
                if(array[j] > temp) {
                    array[j + gap] = array[j];
                } else {
                    break;
                }
            }
            array[j + gap] = temp;
        }
    }
    // 选择排序1
    public static void selectSort(int[] array){
        for (int i = 0 ; i < array.length; i++) {
            int minIndex = i;
            for (int j = i + 1; j < array.length; j++) {
                if(array[minIndex] > array[j]) {
                    minIndex = j;
                }
            }
            swap(array,minIndex,i);
        }
    }


    // 堆排序
    public static void heapSort(int[] array){
        createBigHeap(array);
        int len = array.length - 1;
        for(int i = 0; i <= len; i++) {
            swap(array,0,len - i);
            shiftDown(array, 0, len - i);
        }
    }
    private static void createBigHeap(int[] array) {
        int len = array.length - 1;
        for (int i = (len - 1)/2; i >= 0; i--) {
            shiftDown(array,i, len + 1);
        }
    }
    private static void shiftDown(int[] array, int parent, int len) {
        int child = parent * 2 + 1;
        while(child < len) {
            if(child + 1 < len && array[child] < array[child + 1]) {
                child++;
            }
            if(array[child] > array[parent]) {
                swap(array,child, parent);
                parent = child;
                child = child * 2 + 1;
            } else {
                break;
            }
        }
    }
    //冒泡排序（每次把最大的数放在最后）
    private static void bubbleSort(int[] array) {
        for (int i = 0; i < array.length - 1; i++) {
            boolean flg = false;
            for (int j = 0; j < array.length - i - 1; j++) {
                if(array[j] > array[j + 1]) {
                    swap(array, j, j + 1);
                    flg = true;
                }
            }
            if(!flg) {
                break;
            }
        }
    }

    //快速排序
    private static void quickSort(int[] array) {
        quick(array,0,array.length - 1);
    }
    private static void quick(int[] array, int start, int end) {
        if(start >= end) {
            return;
        }
        int pivot = parttion(array, start, end);

        quick(array, start, pivot - 1);
        quick(array, pivot + 1, end);
    }
    private static int parttion(int[] array, int left, int right) {
        int i = left;
        int temp = array[left];
        while(left < right) {
            while(left < right && array[right] >= temp) {
                right --;
            }
            while(left < right && array[left] <= temp) {
                left ++;
            }
            swap(array, left, right);
        }
        swap(array, left, i);
        return left;
    }

    private static void swap (int[] array, int start, int end) {
        int temp = array[start];
        array[start] = array[end];
        array[end] = temp;
    }


    public static void main(String[] args) {
        //int[] array = {5,40,1,3,8,2,500,7,1,65, 2};
        int[] array = {1,15,2,18,99,3,7,6,1,2};
        // 1 1 2 2 3 5 7 8 40 65 500

/*        insertSort(array);
        System.out.println(Arrays.toString(array));*/

/*        shellSort(array);
        System.out.println(Arrays.toString(array));*/

/*        selectSort(array);
        System.out.println(Arrays.toString(array));*/

/*        heapSort(array);
        System.out.println(Arrays.toString(array));*/

/*        bubbleSort(array);
        System.out.println(Arrays.toString(array));*/

        quickSort(array);
        System.out.println(Arrays.toString(array));

    }
}
